#include "Delay.h"
int count=0;

void Delay_Init(void) {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

    TIM_TimeBaseInitStruct.TIM_Period = 0x03E7;      //重装值999
    TIM_TimeBaseInitStruct.TIM_Prescaler = 71;   //72分频
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	
	TIM_Cmd(TIM2,ENABLE);
}

void TIM2_NVICInit(void){
	//开启标志位
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	
	NVIC_InitTypeDef NVIC_InitStruct;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_Init(&NVIC_InitStruct);
}

void TIM2_IRQHandler(void){
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET){
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
		count++;
	}
}

void Delay_ms(uint16_t ms) {
    count=0;
    while (count < ms);  
}
